<?php
@@ini_set('session.bug_compat_warn', 0);
@@ini_set('session.bug_compat_42', 0);
session_start() ;
if(!isset($_SESSION["login"]) || $_SESSION["login"] == "")
{   
  header("Location: index.php?error=4") ;
}?>

<html>
<head>
<title>Administration</title>
 <link rel="stylesheet" type="text/css" href="style.css" >  
</head>

<body>
<?php require("menu.php");
require("fonctions.php");
$code=$_GET["error"];
$codesap =  $_POST["codesap"];
$nom_dis =  $_POST["nom_dis"];
$manager =  $_POST["manager"];
$reseau =  $_POST["reseau"];
$zones =  $_POST["zones"];
$groupe =  $_POST["groupe"];
?>
<p class="titre">Gestion des &eacute;tablissements</p>

<?php echo error_message($code);?>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>?cur=16" method="post"id="chg_enq" name="chg_f">
<fieldset>
<legend>Recherche</legend>
<table >
<tr>
	<td>CODE SIGA</td>
	<td>
	<select name="codesap" onchange="this.form.submit()">
	<option value="0"></option>
	<?php 
	if(isset($nom_dis) && $nom_dis!=0){
		$sql11="select  codesap from distributeurs where id='$nom_dis'";
		$res11=mysql_query($sql11);
		$r11=mysql_fetch_object($res11);
		$codesap = $r11->codesap;
	}
	
		$sql1="select codesap from distributeurs order by codesap";
	
	$res1=mysql_query($sql1);
	while($r1=mysql_fetch_object($res1)){
	?>
	<option value="<?php echo $r1->codesap;?>" <?php if($r1->codesap=="$codesap") echo "selected";?>><?php echo $r1->codesap;?></option>
	<?php }?>
	</select>
	</td>

	<td>ETABLISSEMENT</td>
	<td>
			<select name="nom_dis" onchange="this.form.submit()">
			<option value="0"></option>
	<?php 
	if($codesap!=0){
		$sql2="select id,nom from distributeurs where codesap='$codesap' order by nom";
	}else{
		$sql2="select id,nom from distributeurs order by nom";
	}
	
	$res2=mysql_query($sql2);
	while($r2=mysql_fetch_object($res2)){
	?>
	<option value="<?php echo $r2->id;?>" <?php if($r2->id=="$nom_dis") echo "selected";?>><?php echo $r2->nom;?></option>
	<?php }?>
	</select>
	</td>

	<td>MANAGER</td>
	<td>	<select name="manager" onchange="this.form.submit()">
	<option value="0"></option>
	<?php $sql3="select * from managers group by nom";
	$res3=mysql_query($sql3);
	while($r3=mysql_fetch_object($res3)){
		?>
	<option value="<?php echo $r3->id;?>" <?php if($r3->id=="$manager") echo "selected";?>><?php echo "$r3->nom"; echo " $r3->prenom";?></option>
	<?php }?>
	</select>
	</td>
	</tr>
<tr>
	<td>GROUPE</td>
	<td>	<select name="groupe" onchange="this.form.submit()">
	<option value="0"></option>
	<?php 
	if(isset($nom_dis) && $nom_dis!=0){
		 $sql4="select  groupe from distributeurs where id='$nom_dis'";
	}elseif($codesap!=0){
		 $sql4="select  groupe from distributeurs where codesap='$codesap'";
	}else{
		$sql4="select distinct groupe from distributeurs order by groupe";
	}
	
	
	$res4=mysql_query($sql4);
	while($r4=mysql_fetch_object($res4)){
	?>
	<option value="<?php echo $r4->groupe;?>" <?php if($r4->groupe=="$groupe") echo "selected";?>><?php echo $r4->groupe;?></option>
	<?php }?>
	</select></td>
	
	<td>ZONE</td>
	<td>	<select name="zones" onchange="this.form.submit()">
	<option value="0"></option>
	<?php 
	if(isset($nom_dis) && $nom_dis!=0){
		$sql55="select  id_zone as id from distributeurs where id='$nom_dis'";
		$res55=mysql_query($sql55);
		$r55=mysql_fetch_object($res55);
		$zones = $r55->id;
	}elseif($codesap!=0){
		 $sql55="select  id_zone as id from distributeurs where codesap='$codesap'";
		 $res55=mysql_query($sql55);
		$r55=mysql_fetch_object($res55);
		$zones = $r55->id;
	}
	
		$sql5="select * from zones";
	
	$res5=mysql_query($sql5);
	while($r5=mysql_fetch_object($res5)){
	?>
	<option value="<?php echo $r5->id;?>" <?php if($r5->id=="$zones") echo "selected";?>><?php echo $r5->descrip;?></option>
	<?php }?>
	</select></td>

	<td>RESEAU</td>
	<td>	<select name="reseau" onchange="this.form.submit()">
	<option value="0"></option>
	<?php 
	if(isset($nom_dis) && $nom_dis!=0){
		$sql66="select  id_reseau  from distributeurs where id='$nom_dis'";
		$res66=mysql_query($sql66);
		$r66=mysql_fetch_object($res66);
		$reseau = $r66->id_reseau;
	}elseif($codesap!=0){
		$sql66="select  id_reseau  from distributeurs where codesap='$codesap'";
		$res66=mysql_query($sql66);
		$r66=mysql_fetch_object($res66);
		$reseau = $r66->id_reseau;
	}
	
	$sql6="select * from reseau";
	$res6=mysql_query($sql6);
	while($r6=mysql_fetch_object($res6)){
	?>
	<option value="<?php echo $r6->id;?>" <?php if($r6->id=="$reseau") echo "selected";?>><?php echo $r6->descrip;?></option>
	<?php }?>
	</select></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
	<td colspan=2><a class="bouton" href="rt_list_dis.php?cur=16">R&eacute;initialiser</a></td>
</tr>
</table>
</fieldset>

<table align="center">
<tr>
	<td colspan="2" align=center><a class="bouton" href="dis_contacts.php?action=new_dis&cur=16&profil=RT">Ajouter DISTRIBUTEUR</a> </td>
<td colspan="2" align=center><a class="bouton" href="rt_add_man.php?action=new_man&cur=16&profil=RT">Ajouter MANAGER</a> </td>
<td colspan="2" align=center><a class="bouton" href="rt_aff_man.php?action=aff_man&cur=16&profil=RT">Affecter MANAGER - DISTRIBUTEUR</a> </td>
</tr>
</table>

<table class="tab_dis" align="center" >
<?php 

if(isset($codesap) && $codesap!=0){
	$sql="select * from distributeurs where codesap='$codesap'";
}elseif(!empty($groupe)){
	$sql="select * from distributeurs where groupe like '%$groupe%'";
}elseif(isset($nom_dis) && $nom_dis!=0){
	$sql="select * from distributeurs where id='$nom_dis'";
}elseif(isset($manager) && $manager!=0){
	$sql="select * from distributeurs where id in (select id_distrib from asso_dis_man where id_man=$manager)";
}elseif(isset($zones) && $zones!=0){
	$sql="select * from distributeurs where id_zone='$zones'";
}elseif(isset($reseau) && $reseau!=0){
	$sql="select * from distributeurs where id_reseau='$reseau'";
}else{
	$sql="select * from distributeurs";
}
 //echo $sql;
	$res = mysql_query($sql);
		
?>
<tr>
	<td class="tab_dis_head">CODE SIGA</td>
	<td class="tab_dis_head">NOM</td>
	<td class="tab_dis_head">GROUPE</td>
	<td class="tab_dis_head">ACTION</td>
</tr>

<?php 
$t_man=array();
while($r = mysql_fetch_object($res)){
	
?>	

<tr>
	<td class="tab_dis_data"><a href="lv_details.php?codesap=<?php echo $r->codesap;?>&cur=3"><?php echo $r->codesap;?></a></td>
	<td class="tab_dis_data"><?php echo $r->nom;?></td>
	<td class="tab_dis_data"><?php echo $r->groupe;?></td>
	<td class="tab_dis_data"><a href="dis_contacts.php?cur=16&action=mod_dis_new&id_dis=<?php echo $r->id;?>&profil=RT">Modifier</a> / <a href="controler.php?action=sup_dis&id=<?php echo $r->id;?>">Supprimer</a></td>
	</tr>
<?php }?>

</table>

</form>
</body>

</html>
